<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 当加载较慢时，显示loading效果 -->
    <div class="ldbox" v-if="ArticleDetail.title === undefined">
      <van-loading color="#1989fa">文章内容疯狂加载中...</van-loading>
    </div>
    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ ArticleDetail.title }}</h1>

        <!-- 用户信息 -->
        <van-cell
          center
          :title="ArticleDetail.aut_name"
          :label="ArticleDetail.pubdate"
        >
          <template #icon>
            <img :src="ArticleDetail.aut_photo" alt="" class="avatar" />
          </template>
          <template #default>
            <div>
              <van-button
                type="info"
                size="mini"
                v-if="ArticleDetail.is_followed"
                @click="followFn(false)"
                >已关注</van-button
              >

              <van-button
                icon="plus"
                type="info"
                size="mini"
                plain
                v-else
                @click="followFn(true)"
                >关注</van-button
              >
              <div class="van-cell__label">
                阅读量:{{ ArticleDetail.read_count || 0 }}
              </div>
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="ArticleDetail.content"></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button
            icon="good-job"
            type="danger"
            size="small"
            v-if="ArticleDetail.attitude === 1"
            @click="likeFn(false)"
          >
            <van-badge :content="ArticleDetail.like_count">已点赞</van-badge>
          </van-button>

          <van-button
            icon="good-job-o"
            type="danger"
            plain
            size="small"
            v-if="ArticleDetail.attitude === 0 || ArticleDetail.attitude === -1"
            @click="likeFn(true)"
          >
            <van-badge :content="ArticleDetail.like_count">点赞</van-badge>
          </van-button>
        </div>
      </div>
      <!-- 评论区 -->
      <div>
        <commentlist
          :collected="ArticleDetail.is_collected"
          @collectFn="CollectFn"
        ></commentlist>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getArticleDetail,
  Followauthor,
  UnFollowauthor,
  Like,
  UnLike,
  Collect,
  UnCollect,
} from '@/api/article/index.js'
import commentlist from './commentlist.vue'
export default {
  name: 'ArticleDetail',
  data() {
    return {
      id: this.$route.query.articleId, // 上一个页面传过来的文章id
      ArticleDetail: {}, // 文章详情数据  attitude  -1无态度, 0未点赞, 1点赞, 是当前登录用户对此文章的态度
    }
  },
  created() {
    this.getarticledetail()
  },
  methods: {
    // 获取文章详情
    async getarticledetail() {
      const { data: res } = await getArticleDetail(this.id)
      if (res.message !== 'OK') {
        return this.$notify({ type: 'danger', message: '获取数据失败' })
      }
      this.ArticleDetail = res.data
    },
    // 关注/取消关注 作者
    async followFn(flag) {
      if (flag) {
        // 点击了关注按钮 调用关注作者接口 展示已关注按钮
        const { data: res } = await Followauthor(this.ArticleDetail.aut_id)
        if (res.message !== 'OK') {
          return this.$notify({ type: 'danger', message: '操作失败' })
        }
        this.$toast({ type: 'success', message: '关注成功', duration: 500 })
        this.ArticleDetail.is_followed = true
      } else {
        // 点击了已关注按钮 调用取消关注作者接口 展示关注按钮
        const { data: res } = await UnFollowauthor(this.ArticleDetail.aut_id)
        this.ArticleDetail.is_followed = false
        console.log(res)
      }
    },
    // 点赞/取消点赞 该作者文章
    async likeFn(flag) {
      if (flag) {
        // 点击了点赞按钮 调用点赞该文章接口 展示已点赞按钮 点赞总数加一
        const { data: res } = await Like(this.ArticleDetail.art_id)
        if (res.message !== 'OK') {
          return this.$notify({ type: 'danger', message: '操作失败' })
        }
        this.ArticleDetail.attitude = 1
        this.ArticleDetail.like_count++
      } else {
        // 点击了已点赞按钮 调用取消点赞该文章接口 展示点赞按钮 点赞总数减一
        const { data: res } = await UnLike(this.ArticleDetail.art_id)
        console.log(res)
        this.ArticleDetail.attitude = 0
        this.ArticleDetail.like_count--
      }
    },
    // 收藏/取消收藏 该作者文章
    async CollectFn(flag) {
      if (flag) {
        // 点击了收藏按钮 调用收藏该文章接口 展示已收藏按钮
        const { data: res } = await Collect(this.ArticleDetail.art_id)
        if (res.message !== 'OK') {
          return this.$notify({ type: 'danger', message: '操作失败' })
        }
        this.$toast({ type: 'success', message: '收藏成功', duration: 500 })
        this.ArticleDetail.is_collected = true
      } else {
        // 点击了取消收藏按钮 调用取消收藏该文章接口 展示收藏按钮
        const { data: res } = await UnCollect(this.ArticleDetail.art_id)
        console.log(res)
        this.ArticleDetail.is_collected = false
      }
    },
  },
  components: {
    commentlist,
  },
}
</script>

<style scoped lang="less">
.ldbox {
  margin-top: 46px;
  text-align: center;
}
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
/deep/ .van-nav-bar__title {
  color: white;
}
/deep/ .van-badge--fixed {
  position: absolute;
  top: -10px;
  right: -5px;
}
</style>
